package com.hsq.servlet;

import com.alibaba.fastjson2.JSON;
import com.hsq.pojo.ClientRequest;
import com.hsq.pojo.PageBean;
import com.hsq.service.ClientRequestService;
import com.hsq.service.impl.ClientRequestServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/clientRequest/*")
public class ClientRequestServlet extends BaseServlet{

    ClientRequestService service = new ClientRequestServiceImpl();

    /**
     * 有条件的分页查询
     * @param request
     * @param response
     * @throws IOException
     */
    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取数据
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        BufferedReader reader = request.getReader();
        String parms = reader.readLine();

        ClientRequest clientRequest = JSON.parseObject(parms, ClientRequest.class);

//        System.out.println(clientRequest);

        // 调用方法获取页面对象
        PageBean<ClientRequest> pageBean = service.selectByPageAndCondition(currentPage, pageSize, clientRequest);

//        System.out.println(pageBean.getRows());

        // 将页面对象转为json类型
        String jsonString = JSON.toJSONString(pageBean);

//        System.out.println("jsonString"+jsonString);

        // 设置相应类型格式，以及响应内容
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }


    /**
     * 根据id删除数据
     * @param request
     * @param response
     * @throws IOException
     */
    public void deleteById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求数据
        String _id = request.getParameter("id");
        int id = Integer.parseInt(_id);

        // 调用方法删除数据
        service.deleteById(id);

        // 设置响应数据
        response.getWriter().write("success");
    }


    /**
     * 添加数据
     * @param request
     * @param response
     * @throws IOException
     */
    public void add(HttpServletRequest request, HttpServletResponse response) throws IOException {

        BufferedReader reader = request.getReader();
        String params = reader.readLine();

        ClientRequest clientRequest = JSON.parseObject(params, ClientRequest.class);

        service.add(clientRequest);

        response.getWriter().write("success");
    }


    /**
     * 根据id数组删除数据
     * @param request
     * @param response
     * @throws IOException
     */
    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求数据
        BufferedReader reader = request.getReader();
        String params = reader.readLine();

        int[] ids = JSON.parseObject(params, int[].class);

        // 调用方法删除数据
        service.deleteByIds(ids);

        // 设置响应数据
        response.getWriter().write("success");
    }


    /**
     * 根据id查询数据
     * @param request
     * @param response
     * @throws IOException
     */
    public void selectById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求数据
        String _id = request.getParameter("id");
        int id = Integer.parseInt(_id);

        // 调用方法
        ClientRequest clientRequest = service.selectById(id);
        String jsonString = JSON.toJSONString(clientRequest);

        // 设置响应数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }



    public void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求数据
        BufferedReader reader = request.getReader();
        String params = reader.readLine();

        ClientRequest clientRequest = JSON.parseObject(params, ClientRequest.class);

        service.update(clientRequest);

        // 设置响应数据
        response.getWriter().write("success");
    }



    public void run(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求数据id
        String _id = request.getParameter("id");
        int id = Integer.parseInt(_id);

        // 调用方法
        String resp_data = service.run(id);

        // 设置响应数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(resp_data);
    }

}
